package com.yonyou.pmclouds.mob.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.yonyou.pmclouds.basecom.conf.AliTokenProperties;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
//@Controller
@RequestMapping(value="/mobile")
public class AliTokenController {

	@Autowired
	private AliTokenProperties aliTokenProperties;

	@RequestMapping(value="/getAliTokenInfo")
	public void getAliTokenInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		JSONObject obj = new JSONObject();
		log.error("APPTokenServlet request begin.....");
		try {
			String ossType = aliTokenProperties.getOssType();
			String accessKeyId = aliTokenProperties.getAccessKeyID();
			String accessKeySecret = aliTokenProperties.getAccessKeySecret();
			String roleArn = aliTokenProperties.getRoleArn();
			long durationSeconds = Long.parseLong(aliTokenProperties.getTokenExpireTime());

			String roleSessionName = "pmcloud-001";

//			ProtocolType protocolType = ProtocolType.HTTPS;
//			AssumeRoleResponse stsResponse = assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName, "",
//					protocolType, durationSeconds);

			obj.put("status", "200");
			obj.put("OssType", ossType);
			obj.put("AccessKeyId", accessKeyId);
			obj.put("AccessKeySecret", accessKeySecret);
//			obj.put("SecurityToken", stsResponse.getCredentials().getSecurityToken());
//			obj.put("Expiration", stsResponse.getCredentials().getExpiration());

			obj.put("Endpoint", aliTokenProperties.getEndpoint());
			obj.put("Bucketname", aliTokenProperties.getBucketname());
			obj.put("Region", aliTokenProperties.getRegion());
			obj.put("Connectiontimeout", aliTokenProperties.getConnectiontimeout());
			obj.put("Sockettimeout", aliTokenProperties.getSockettimeout());
			obj.put("Maxconcurrentrequest", aliTokenProperties.getMaxconcurrentrequest());
			obj.put("Maxerrorretry", aliTokenProperties.getMaxerrorretry());

			response(response, obj.toString());
			log.error("APPTokenServlet request successfull.....");
		} catch (Exception e) {
			log.error("APPTokenServlet request fail.....");
			log.error(" error infomation :" + e.getMessage());
			try {
				obj.put("status", "500");
				obj.put("OssType", "");
				obj.put("AccessKeyId", "");
				obj.put("AccessKeySecret", "");
				obj.put("SecurityToken", "");
				obj.put("Expiration", "");
				obj.put("Endpoint", "");
				obj.put("Bucketname", "");
				obj.put("Connectiontimeout", "");
				obj.put("Sockettimeout", "");
				obj.put("Maxconcurrentrequest", "");
				obj.put("Maxerrorretry", "");
			} catch (JSONException Je) {
				log.error("APPTokenServlet request fail.....");
				log.error(" error infomation :" + Je.getMessage());
			}
			response(response, obj.toString());
		}

	}

	private void response(HttpServletResponse response, String results) throws IOException {
		
		response.getWriter().println(results);
		response.setStatus(200);
		response.flushBuffer();
	}

	private AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret, String roleArn,
			String roleSessionName, String policy, ProtocolType protocolType, long durationSeconds)
			throws ClientException {
		IClientProfile profile;
		try {
			profile = DefaultProfile.getProfile(aliTokenProperties.getRegion(), accessKeyId, accessKeySecret);
			DefaultAcsClient client = new DefaultAcsClient(profile);
			AssumeRoleRequest request = new AssumeRoleRequest();
			request.setVersion(aliTokenProperties.getApiversion());
			request.setMethod(MethodType.POST);
			request.setProtocol(protocolType);
			request.setRoleArn(roleArn);
			request.setRoleSessionName(roleSessionName);

			request.setDurationSeconds(Long.valueOf(durationSeconds));
			AssumeRoleResponse response = client.getAcsResponse(request);

			return response;
		} catch (ClientException e) {
			throw e;
		}
	}

}
